package com.wtwd.device.controller;


import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.dto.SynchronizationSleepDto;
import com.wtwd.device.model.dto.SynchronizationStepDto;
import com.wtwd.device.model.entity.DeviceBloodOxygenTotal;
import com.wtwd.device.model.entity.DeviceSleepRecord;
import com.wtwd.device.model.entity.DeviceSleepRecordInfo;
import com.wtwd.device.model.entity.DeviceStepInfo;
import com.wtwd.device.model.vo.DeviceBloodOxygenTotalVo;
import com.wtwd.device.model.vo.DeviceSleepRecordVo;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.service.AppDeviceSleepRecordService;
import com.wtwd.device.util.DateUtils;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Api(tags = "设备睡眠模块")
@RestController
@RequestMapping("/app-device-sleep")
public class AppDeviceSleepController {


    @Resource
    private AppDeviceSleepRecordService appDeviceSleepRecordService;

    @ApiOperation("获取某一天的睡眠信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "date", value = "日期,yyyyMMdd", required = true, dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "参数错误")
    })
    @GetMapping("/getDayDeviceSleepByTime")
    public ResultVO<DeviceSleepRecordVo> getDayDeviceSleepByTime(@RequestParam String date, @RequestParam Long deviceId) {
        DeviceSleepRecordVo deviceHeartRateTotalVo = appDeviceSleepRecordService.getDayDeviceSleepByTime(deviceId, date);
        return ResultVO.success(deviceHeartRateTotalVo);
    }


    @ApiOperation(value = "根据开始时间和结束时间获取这个时间段的睡眠信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备id", required = true, dataType = "long")
            , @ApiImplicitParam(name = "startDate", value = "开始日期,yyyyMMdd", required = true, dataType = "String")
            , @ApiImplicitParam(name = "endDate", value = "结束日期,yyyyMMdd", required = true, dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "参数错误")
    })
    @GetMapping("/getDaysDeviceSleep")
    public ResultVO<List<DeviceSleepRecordVo>> getDaysDeviceSleep(@RequestParam Long deviceId, @RequestParam String startDate, @RequestParam String endDate) {
        List<DeviceSleepRecordVo> deviceBloodOxygenTotals = appDeviceSleepRecordService.getDaysDeviceSleep(deviceId, startDate, endDate);
        return ResultVO.success(deviceBloodOxygenTotals);
    }

    /**
     * 同步睡眠数据
     *
     * @return
     */
    @ApiOperation("同步睡眠数据")
    @PostMapping("/synchronizationSleep")
    public ResultVO<DeviceSleepRecord> synchronizationSleep(@RequestBody SynchronizationSleepDto dao) {
        if (appDeviceSleepRecordService.synchronizationSleep(dao.getDeviceSleepDtos())){
            DeviceSleepRecord deviceSleepRecord = appDeviceSleepRecordService.getLastDeviceSleepByDeviceId(dao.getDeviceId());
            return ResultVO.success(deviceSleepRecord);
        }
        return ResultVO.error();
    }

}
